﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestDesc.Question;

namespace LongestDesc.Answer
{
    class Effie : ILongestDesc
    {
        public bool IsDesc(int[] arr)
        {
            for (int i = 0; i < arr.Length - 1; i++)
            {
                if (arr[i] < arr[i + 1])
                    return false;
            }
            if (arr[arr.Length - 2] < arr[arr.Length - 1])
                return false;
            return true;
        }
        public int GetLongestDesc(int[] iarr)
        {
            if (iarr.Length == 0)
                return 0;
            int tem = 1;
            int flag = iarr[0];
            for (int i = 1; i < iarr.Length; i++)
            {
                if (iarr[i] > flag)
                {
                    if (tem>1 &&i + tem < iarr.Length)
                    {
                        int[] temparry = new int[tem];
                        Array.Copy(iarr, i, temparry, 0, tem);
                        if (IsDesc(temparry))
                        {
                            tem += 1;
                            i = i + tem;
                        }
                    }
                }
                else
                {
                    if (i + 2 <= iarr.Length)
                    {
                        if (iarr[i] < iarr[i + 1])
                        {
                            int[] temparry = new int[3];
                            temparry[0] = iarr[i - 1];
                            Array.Copy(iarr, i + 1, temparry, 1, 2);
                            if (IsDesc(temparry))
                            {
                                tem += 1;
                                i = i + 2;
                            }
                        }
                    }

                        flag = iarr[i];
                        tem += 1;
                }
            }
            return tem;
        }
    }
}
